﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using log4net;
using System.Collections;

namespace Cn.Edwon.Web.HtmlControl {
    public class RichSelect {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 空数据
        /// </summary>
        /// <returns></returns>
        public const String EmptyContent = "[[\"\",\"错误的服务类型\"]]";
        /// <summary>
        /// 获取单个EO的列表，并转化为下拉列表可用的json数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rEOName"></param>
        /// <param name="rKeyFieldName"></param>
        /// <param name="rValueFeildName">多个字段使用逗号分隔</param>
        /// <returns></returns>
        public static String ListSEO2DropDownList<T>(IList<T> list, String rKeyFieldName, String rValueFeildName) {
            return ListSEO2DropDownList<T>(list, rKeyFieldName, rValueFeildName, null);
        }
        /// <summary>
        /// 获取单个EO的列表，并转化为下拉列表可用的json数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rEOName"></param>
        /// <param name="rKeyFieldName"></param>
        /// <param name="rValueFeildName">多个字段使用逗号分隔</param>
        /// <param name="rProperties">多个字段使用逗号分隔</param>
        /// <returns></returns>
        public static String ListSEO2DropDownList<T>(IList<T> list, String rKeyFieldName, String rValueFeildName, String rProperties) {
            String[] valueFeilds = rValueFeildName.Split(',');
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            if (list != null) {
                Object key = null;
                Object val = null;
                Object prop = null;
                for (Int32 i = 0; i < list.Count; i++) {
                    Object item = Activator.CreateInstance(typeof(T));
                    key = typeof(T).GetProperty(rKeyFieldName).GetValue(list[i], null);
                    if (valueFeilds.Length == 0) {//没有显示的字段
                        val = "/";
                    } else if (valueFeilds.Length == 1) {//只显示一个text字段
                        val = typeof(T).GetProperty(valueFeilds[0]).GetValue(list[i], null);
                    } else {//显示多个text字段
                        String valStr = "";
                        val = typeof(T).GetProperty(valueFeilds[0]).GetValue(list[i], null);
                        if (val != null) {
                            valStr = val.ToString();
                            valStr += "(";
                            for (int j = 1; j < valueFeilds.Length; j++) {
                                if (j > 1)
                                    valStr += ",";
                                val = typeof(T).GetProperty(valueFeilds[j]).GetValue(list[i], null);
                                if (val != null) {
                                    valStr += val.ToString();
                                } else {
                                    valStr += "--";
                                    //log.Error("ListSEO2DropDownList:获取属性 " + rValueFeildNames[j] + " 的值时出现错误");
                                }
                            }
                            valStr += ")";
                        } else {
                            valStr = "/";
                            //log.Error("ListSEO2DropDownList:获取属性 "+rValueFeildNames[0]+" 的值时出现错误");
                        }
                        val = valStr;
                    }
                    String propStr = "";
                    if (rProperties != null) {
                        String[] props = rProperties.Split(',');
                        Int32 propCount = 0;
                        foreach (String p in props) {
                            prop = typeof(T).GetProperty(p).GetValue(list[i], null);
                            if (prop != null) {
                                if (propCount > 0) {
                                    propStr += ",";
                                }
                                propStr += "\"" + p + "\":\"" + prop.ToString() + "\"";
                                propCount++;
                            }
                        }
                    }
                    if (key == null || val == null) continue;
                    if (i == 0) {
                        sb.Append("[\"" + key + "\",\"" + val + "\",{" + propStr + "}]");
                    } else {
                        sb.Append(",[\"" + key + "\",\"" + val + "\",{" + propStr + "}]");
                    }
                }
            }
            sb.Append("]");
            return sb.ToString();
        }
    }
}
